package com.dpstudio.ymprestlful.configuration;

import com.dpstudio.ymprestlful.beans.AppSetting;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.util.xmlb.XmlSerializerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * @Author: mengxiang.
 * @Date: 2020/6/22.
 * @Time: 11:37 上午.
 * @Description:
 */
@State(name = "YmpRestfulSetting", storages = @Storage("YmpRestfulSetting.xml"))
public class YmpRestfulSetting implements PersistentStateComponent<YmpRestfulSetting> {

    private final AppSetting setting;

    public YmpRestfulSetting() {
        this.setting = new AppSetting();
        this.setting.initValue();
    }

    public static YmpRestfulSetting getInstance() {
        return ServiceManager.getService(YmpRestfulSetting.class);
    }

    public boolean isModified(AppSetting setting) {
        if (setting == null) {
            return false;
        }
        return this.setting.isModified(setting);
    }

    @Nullable
    @Override
    public YmpRestfulSetting getState() {
        return this;
    }

    @Override
    public void loadState(@NotNull YmpRestfulSetting state) {
        XmlSerializerUtil.copyBean(state, this);
    }

    @NotNull
    public AppSetting getAppSetting() {
        return this.setting;
    }

    public void setAppSetting(AppSetting setting) {
        this.setting.applySetting(setting);
    }
}
